/*
 * @Author: szx
 * @Date: 2021-11-22 14:46:22
 * @LastEditTime: 2021-11-22 15:03:43
 * @Description:
 * @FilePath: \leetcode\200-299\213\213.js
 */
/**
 * @param {number[]} nums
 * @return {number}
 */
const rob = function (nums) {
    const n = nums.length;
    if (n == 1) {
        return nums[0];
    } else if (n == 2) {
        return Math.max(nums[0], nums[1]);
    }
    return Math.max(func(nums, 0, n - 1), func(nums, 1, n));
};
const func = function (nums, start, end) {
    const n = nums.length;
    let q = 0,
        p = 0,
        r = nums[start];

    for (let i = start + 1; i < end; i++) {
        q = p;
        p = r;
        r = Math.max(p, q + nums[i]);
    }
    return Math.max(r, p);
};
